/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtCore>
#include <QMessageBox>
#include <QKeyEvent>

#include "qtvomp.h"
#include "widgettimers.h"
#include "timerlistdelegate.h"
#include "dlgsettimer.h"

WidgetTimers::WidgetTimers( MainWindow* mainWindow, QWidget *parent ) : QWidget( parent ),
    m_mainWindow( mainWindow )
{
    m_ui.setupUi( this );

    m_ui.backButton->setIcon( style()->standardIcon( QStyle::SP_ArrowLeft ) );

    TimerListDelegate* timerDelegate = new TimerListDelegate();
    m_ui.listTimers->setModel( m_mainWindow->timerListModel() );
    m_ui.listTimers->setItemDelegate( timerDelegate );

    m_ui.listTimers->setSelectionModel( m_mainWindow->timerListSelectionModel() );
    m_ui.listTimers->setSelectionMode( QAbstractItemView::SingleSelection );
    m_ui.listTimers->setSelectionBehavior( QAbstractItemView::SelectRows );

    connect( m_ui.backButton,           SIGNAL( clicked() ),
             this,                      SLOT( backClicked() ) );

    connect( m_ui.editButton,           SIGNAL( clicked() ),
             this,                      SLOT( editClicked() ) );

    connect( m_ui.deleteButton,         SIGNAL( clicked() ),
             this,                      SLOT( deleteClicked() ) );

    connect( this,                      SIGNAL( deleteTimer( TimerEntry ) ),
             mainWindow->vdr(),         SLOT( deleteTimer( TimerEntry ) ) );

    connect( mainWindow->serverTimer(), SIGNAL( timeout() ),
             this,                      SLOT( updateTime() ) );
}


WidgetTimers::~WidgetTimers()
{
}


void WidgetTimers::updateTime()
{
    m_ui.time->setText( m_mainWindow->serverTime().toString( "dddd - d MMMM yyyy - hh:mm" ) );
}


void WidgetTimers::initScreen()
{
    updateTime();
}


void WidgetTimers::backClicked()
{
    emit back();
}


void WidgetTimers::editClicked()
{
    QModelIndexList selectionList = m_mainWindow->timerListSelectionModel()->selectedIndexes();
    if ( selectionList.size() == 1 )
    {
        int row = selectionList[ 0 ].row();

        if ( row < m_mainWindow->timerList()->size() )
        {
            TimerEntry timer = m_mainWindow->timerList()->at( row );

            DlgSetTimer dlg( m_mainWindow, timer, DlgSetTimer::Edit, this );
            dlg.exec();
        }
        else
        {
            qWarning() << "WidgetTimers::editClicked: Wrong channel list size: " << m_mainWindow->channelList()->size();
        }
    }
    else
    {
        qWarning() << "WidgetTimers::editClicked: Wrong selection list size: " << selectionList.size();
    }
}


void WidgetTimers::deleteClicked()
{
    QModelIndexList selectionList = m_mainWindow->timerListSelectionModel()->selectedIndexes();
    if ( selectionList.size() == 1 )
    {
        int row = selectionList[ 0 ].row();

        if ( row < m_mainWindow->timerList()->size() )
        {
            TimerEntry timer = m_mainWindow->timerList()->at( row );

            if ( QMessageBox::question( this,
                                        "Delete timer",
                                        "Are you sure you want to delete this timer?",
                                        QMessageBox::Yes | QMessageBox::No,
                                        QMessageBox::No ) == QMessageBox::Yes )
            {
                emit deleteTimer( timer );
            }
        }
        else
        {
            qWarning() << "WidgetTimers::deleteClicked: Wrong channel list size: " << m_mainWindow->channelList()->size();
        }
    }
    else
    {
        qWarning() << "WidgetTimers::deleteClicked: Wrong selection list size: " << selectionList.size();
    }
}
